﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;

namespace KL.Store.Core
{
    public class Utils
    {
        #region SendMail

        public static void SendMail(string to, string cc, string bcc, string subject, string body, bool IsHtml, HttpPostedFileBase file = null)
        {
            System.Net.Mail.MailAddress gonderen = new System.Net.Mail.MailAddress(Parameters.EMail.FromEmail, Parameters.EMail.FromName);
            System.Net.Mail.MailAddress alan = new System.Net.Mail.MailAddress(to);
            System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(gonderen, alan);
            mm.BodyEncoding = System.Text.Encoding.UTF8;


            mm.IsBodyHtml = IsHtml;
            //mm.Body = HtmlTurkishCharsEncode(HttpUtility.HtmlDecode(body));
            mm.Body = HttpUtility.HtmlDecode(body);
            mm.Subject = subject;
            if (file != null)
            {
                string fileName = Path.GetFileName(file.FileName);
                Attachment attachment = new Attachment(file.InputStream, fileName);
                mm.Attachments.Add(attachment);
            }
            if (!string.IsNullOrEmpty(cc))
            {
                if (cc.Contains(";"))
                {
                    foreach (var cca in cc.Split(';'))
                    {
                        mm.CC.Add(new System.Net.Mail.MailAddress(cca.Trim()));
                    }
                }
                else
                {
                    mm.CC.Add(new System.Net.Mail.MailAddress(cc.Trim()));
                }
            }
            if (!string.IsNullOrEmpty(bcc))
            {
                if (bcc.Contains(";"))
                {
                    foreach (var bcca in bcc.Split(';'))
                    {
                        mm.Bcc.Add(new System.Net.Mail.MailAddress(bcca.Trim()));
                    }
                }
                else
                {
                    mm.Bcc.Add(new System.Net.Mail.MailAddress(bcc.Trim()));
                }
            }

            SmtpClient smtp = new SmtpClient(Parameters.EMail.Smtp, Convert.ToInt32(Parameters.EMail.Port));
            smtp.Credentials = new NetworkCredential(Parameters.EMail.UserName, Parameters.EMail.Password);
            smtp.EnableSsl = Parameters.SmtpSSLRequired;
            smtp.Send(mm);
        }

        #endregion

        #region IsValidEmail

        public static bool IsValidEmail(string email)
        {
            string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
            + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
            + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";

            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);

            return regex.IsMatch(email);
        }

        #endregion

        #region EncodeProductUrl

        public static string EncodeProductUrl(string url)
        {
            return "/" + HttpUtility.UrlEncode(url.Replace("  ", "_").Replace(" ", "-").Replace(":", "-").Replace("%", "").Replace("+", "").Replace("/", "-").Replace("&", "_ve_").Replace("'", "").Replace(@"""", ""), System.Text.Encoding.GetEncoding("ISO-8859-1"));
        }

        #endregion

        #region IsProxyUser

        public static bool IsProxyUser
        {
            get
            {
                String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                return ip != null && ip.ToLower() != "unknown";
            }
        }
        #endregion

        #region UserIP

        public static string UserIP
        {
            get
            {
                try
                {
                    string ip = String.Empty;

                    if (IsProxyUser)
                        ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    else
                    {
                        ip = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
                        if (string.IsNullOrEmpty(ip))
                            ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }

                    if (ip == "::1") ip = "127.0.0.1";

                    return ip;
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
        }

        #endregion

        public static string HtmlTurkishChars(string value)
        {
            return value.Replace("&#304;", "İ").Replace("&#305;", "ı").Replace("&#214;", "Ö").Replace("&#246;", "ö").Replace("&#220;", "Ü").Replace("&#252;", "ü").Replace("&#199;", "Ç").Replace("&#231;", "ç").Replace("&#286;", "Ğ").Replace("&#287;", "ğ").Replace("&#350;", "Ş").Replace("&#351;", "ş");

        }

        public static string HtmlTurkishCharsEncode(string value)
        {
            return value.Replace("İ", "&#304;").Replace("ı", "&#305;").Replace("Ö", "&#214;").Replace("ö", "&#246;").Replace("Ü", "&#220;").Replace("ü", "&#252;").Replace("Ç", "&#199;").Replace("ç", "&#231;").Replace("Ğ", "&#286;").Replace("ğ", "&#287;").Replace("Ş", "&#350;").Replace("ş", "&#351;");

        }

        public static string SefText(string url)
        {
            return url.Replace(" - ", "-").Replace(" ", "-").Replace("&", "ve").Replace("\"", "").Replace("'", "").Replace("/", "-").Replace("ş", "s").Replace("ü", "u").Replace("ö", "o").Replace("ı", "i").Replace("ç", "c").Replace("ğ", "g").Replace("/", "-").Replace("+", "").Replace("%", "").Replace(":", "-").Replace("*", "x").Replace("<!--br-->", "").Replace("#", "").Replace("	", "").ToLower();
        }

    }
}
